package org.ume.school.modules.comment;

import org.ume.school.modules.model.entity.Thumbup;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Django on 2017/7/22.
 */
@Service
public class ThumbUpService {

    @Resource
    private ThumbupRepository thumbupRepository;

    @Transactional
    public Thumbup thumbup(Thumbup thumbup) {
        if (null == thumbup) {
            return null;
        }
        //默认值为1
        thumbup.setThumbUp(1);
        List<Thumbup> list = thumbupRepository.findByWhoAndGoodsId(thumbup.getWho(), thumbup.getGoodsId());
        if (null != list && list.size() > 0) {
            Thumbup thumbup1 = list.get(0);
            long m = thumbup1.getCreateTime().getTime() / 1000;
            long n = System.currentTimeMillis() / 1000;
            boolean ok = (n - m) > 60 * 60 * 24L;
            if (ok) {
                thumbup.setThumbUp(thumbup1.getThumbUp() + 1);
                thumbup.setId(thumbup1.getId());
            } else { // 有点过赞，但未满24小时，不能连续点赞
                return null;
            }
        }
        thumbup.setCreateTime(new Date());
        Thumbup effectRow = thumbupRepository.saveAndFlush(thumbup);
        return effectRow;
    }

}
